package com.chocus.core.service.buyer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import com.chocus.common.web.Constants;

import redis.clients.jedis.Jedis;

public class SessionProviderImpl implements SessionProvider {

	@Autowired
	@Qualifier("jedis")
	private Jedis jedis;
	int seconds = 30;

	public void setSeconds(int seconds) {
		this.seconds = seconds;
	}

	@Override
	public void setAttribute(String key, String value) {
		// 设置session
		jedis.set(key + ":" + Constants.BUYER_SESSION, value);
		// 设置超时时间
		jedis.expire(key + ":" + Constants.BUYER_SESSION, seconds * 60);
	}

	@Override
	public String getAttribute(String key) {
		String session = jedis.get(key + ":" + Constants.BUYER_SESSION);
		if (session != null) {
			// 设置超时时间
			jedis.expire(key + ":" + Constants.BUYER_SESSION, seconds * 60);
			return session;
		}
		return null;
	}

	@Override
	public void removeAttribute(String key) {
		jedis.del(key + ":" + Constants.BUYER_SESSION);
	}

}
